﻿using System.Collections.Generic;
using System.ComponentModel;
using System.Net.Mail;
using System.Text;
using System.Web;

namespace BLWCMS.Core.Helpers
{
    public class CommonHelper
    {
        #region 将布尔值转换为数值
        /// <summary>
        /// 将布尔值转换为数值
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static int ChangeBoolToInt(bool value)
        {
            return value ? 1 : 0;
        }
        #endregion

        #region 发送邮件服务
        /// <summary>
        /// 发送邮件服务
        /// </summary>
        /// <param name="smtpHost">SMTP服务地址</param>
        /// <param name="smtpPort">SMTP服务端口</param>
        /// <param name="smtpAccount">SMTP账号</param>
        /// <param name="smtpPassword">SMTP密码</param>
        /// <param name="mailSubject">邮件主题</param>
        /// <param name="mailFromAddress">邮件发送者地址</param>
        /// <param name="mailDisplayName">邮件发送者显示名称</param>
        /// <param name="mailToList">收件人列表</param>
        /// <param name="mailBody">邮件主题</param>
        public static void MailService(string smtpHost, int smtpPort, string smtpAccount, string smtpPassword,
            string mailSubject, string mailFromAddress, string mailDisplayName, List<string> mailToList, string mailBody)
        {
            SmtpClient client = new SmtpClient();

            //设置用于 SMTP 事务的主机的名称，填IP地址也可以
            client.Host = smtpHost;

            //设置用于 SMTP 事务的端口，默认的是 25，SSL安全连接默认994
            client.Port = smtpPort;

            //设置用于 SMTP 验证的信息
            client.UseDefaultCredentials = false;
            client.Credentials = new System.Net.NetworkCredential(smtpAccount, smtpPassword);
            client.DeliveryMethod = SmtpDeliveryMethod.Network;

            MailMessage mail = new MailMessage();

            //设置邮件的主题
            mail.Subject = mailSubject;

            //设置邮件发件人
            mail.From=new MailAddress(mailFromAddress,mailDisplayName,Encoding.UTF8);

            ////设置邮件接收人
            foreach (string address in mailToList)
            {
                mail.To.Add(new MailAddress(address));
            }

            //设置邮件内容
            mail.Body = mailBody;

            //设置邮件的格式
            mail.BodyEncoding = System.Text.Encoding.UTF8;
            mail.IsBodyHtml = true;

            //设置邮件发送级别
            mail.Priority = MailPriority.Normal;
            mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnSuccess;

            client.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);

            client.SendAsync(mail, null);
            //client.Send(mail);
        }

        private static void SendCompletedCallback(object sender, AsyncCompletedEventArgs e)
        {
            HttpContext context = HttpContext.Current;
            if (e.Error != null)
            {
                context.Response.Write(ResCode.M500());
            }
            else
            {
                context.Response.Write(ResCode.M200());
            }
        }
        #endregion
    }
}
